
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[
    ],
    formData:{
      position: 0,
      text: '',
      color: '',
      rate: 1.0,
      direction: 0
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    zh: '',
    videoUrl: getApp().globalData.videoUrl
  },
  onLoad(options) {
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    const schema = [
      {
        label:'跳转到的位置，单位 s',
        prop:'position',
        type:'number',
        value: 0
      },
      {
        label:'弹幕文字',
        prop:'text',
        type:'text',
        value: ''
      },
      {
        label:'弹幕颜色',
        prop:'color',
        type:'text',
        value: '',
        placeholder: '例如：#ff6600'
      },
      {
        label:'倍速播放',
        prop:'rate',
        type:'picker',
        labelOptions:['0.5','0.8','1.0','1.25','1.5','2.0', 3.0, '4.0'],
        valueOptions:[0.5, 0.8,1.0,1.25,2.0, 3.0, 4.0],
        value: 1.0
        // 倍率，支持 0.5/0.8/1.0/1.25/1.5，2.6.3 起支持 2.0 倍速，3.7.5 起支持 3.0/4.0 倍速
      },
      {
        label:'全屏时视频的方向',
        prop:'direction',
        type:'picker',
        labelOptions:['正常竖向(0)','屏幕逆时针90度(90)','屏幕顺时针90度(-90)'],
        valueOptions:[0, 90, -90]
        // 设置全屏时视频的方向，不指定则根据宽高比自动判断
      },
    ]
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl,
      schema: schema.filter(item => {
        // 
        if(options.zh === 'VideoContext.seek') {
          return item.prop === 'position'
        }
        if(options.zh === 'VideoContext.sendDanmu') {
          return item.prop === 'text' || item.prop === 'color'
        }
        if(options.zh === 'VideoContext.playbackRate') {
          return item.prop === 'rate'
        }
        return false;
      })
    })
    this.videoContext = wx.createVideoContext('the-id', this);
  },
  getFormData(e) {
    const formData = {}
    this.data.schema.forEach(item => {
      if(item.type === 'number') {
        if(!isNaN(+e.detail.formData[item.prop]) && e.detail.formData[item.prop] !== '') {
          formData[item.prop] = +e.detail.formData[item.prop]
        }
      } else {
        formData[item.prop] = e.detail.formData[item.prop]
      }
    })
    this.setData({
      formData: formData
    });
    console.log('formData', formData);
  },
  choose1() {
    if(this.videoContext) {
      this.videoContext.play()
    }
  },
  choose2() {
    if(this.videoContext) {
      this.videoContext.pause()
    }
  },
  choose3() {
    if(this.videoContext) {
      this.videoContext.stop()
    }
  },
  choose4() {
    if(this.videoContext) {
      this.videoContext.seek(this.data.formData.position)
    }
  },
  choose5() {
    if(this.videoContext) {
      this.videoContext.sendDanmu({
        text: this.data.formData.text,
        color: this.data.formData.color,
      })
    }
  },
  choose6() {
    if(this.videoContext) {
      // 倍率，支持 0.5/0.8/1.0/1.25/1.5，2.6.3 起支持 2.0 倍速，3.7.5 起支持 3.0/4.0 倍速
      this.videoContext.playbackRate(this.data.formData.rate)
    }
  },
  choose7() {
    if(this.videoContext) {
      this.videoContext.requestFullScreen(
        this.data.formData.direction === undefined ? {} : {
          direction: this.data.formData.direction
        }
      )
    }
  },
  choose8() {
    if(this.videoContext) {
      this.videoContext.exitFullScreen()
    }
  },
  choose9() {
    if(this.videoContext) {
      this.videoContext.showStatusBar()
    }
  },
  choose10() {
    if(this.videoContext) {
      this.videoContext.hideStatusBar()
    }
  },
  choose11() {
    this.videoContext.play()
    const that = this;
    setTimeout(() => {
      wx.navigateTo({
        url: '/packageAPI/pages/videomedia/video-context-two',
        success(res) {
          try {
            res.eventChannel.emit('acceptDataFromOpenedPage', {
              data: that.videoContext
            })
          } catch (err) {
          }
        }
      })
    }, 1000);
  },
  choose12() {
    if(this.videoContext) {
      this.videoContext.requestBackgroundPlayback()
    }
  },
  choose13() {
    if(this.videoContext) {
      this.videoContext.exitBackgroundPlayback()
    }
  },
  onUnload() {
  }
})